package com.zpark.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;

@Configuration
public class RedisTemplateConfig {
    /**
     * 创建RedisTemplate对象，交给spring工厂管理
     * @param redisConnectionFactory 需要的redisConnection工厂，因为有注解@Bean，这个形成会由spring工厂自动装配
     * @return RedisTemplate对象
     */
    @Bean("redisTemplate")
    public RedisTemplate getRedisTemplate(RedisConnectionFactory redisConnectionFactory){
        //创建一个对象
        RedisTemplate redisTemplate=new RedisTemplate();

        //指定需要的redisConnection工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        //key的序列化方式通过json完成
        redisTemplate.setKeySerializer(new GenericJackson2JsonRedisSerializer());

        //value的序列化方式通过json完成
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        return redisTemplate;
    }
}
